package cn.edu.nchu.fragmentdemo.fragments;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 16035
 */

public class FlightInformation extends View {
    /** 飞行时间 */
    private float flightTime;
    /** 距离 */
    private float distance;
    /** 高度 */
    private float height;
    /** 垂直速度 */
    private float verticalSpeed;
    /** 水平速度 */
    private float horizontalSpeed;
    /** 经度 */
    private float lat;
    /** 纬度 */
    private float lng;
    /** 统一字体大小 */
    private float textSize;

    public FlightInformation(Context context, AttributeSet attrs) {
        super(context, attrs);
        //初始化验证数据
        flightTime = 0;
        distance = 10;
        height = 0;
        verticalSpeed = 20;
        horizontalSpeed = (float)(30.415);
        lat = 0;
        lng = 0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        textSize = getHeight()/10;
        //每个显示之间的间隔
        float spacing = textSize+10;
        //起始位置
        float startX = 10;
        float startY = textSize;

        Paint bluePaint = new Paint();
        bluePaint.setTextSize(textSize);
        bluePaint.setAntiAlias(true);
        bluePaint.setFakeBoldText(true);
        bluePaint.setColor(Color.rgb(3,1,238));

        Paint orangePaint = new Paint();
        orangePaint.setTextSize(textSize);
        orangePaint.setAntiAlias(true);
        orangePaint.setFakeBoldText(true);
        orangePaint.setColor(Color.rgb(232,120,81));

        // 创建一个 SimpleDateFormat 对象，指定时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        // 创建一个 Date 对象，将秒数转换为毫秒数
        Date date = new Date((long) (flightTime * 1000));
        // 使用 SimpleDateFormat 对象将 Date 对象格式化为时间字符串
        String flightTime = sdf.format(date);
        //设置飞行时间相关文本
        canvas.drawText("飞行时间",startX,startY,orangePaint);
        canvas.drawText(flightTime,startX,startY + spacing,orangePaint);

        //设置距离相关文本
        String distanceText = "D" + "  " + String.format("%.1f",distance) + " m";
        canvas.drawText(distanceText,startX,startY + spacing*2,bluePaint);

        //设置高度相关文本
        String heightText = "H" + "  " + String.format("%.1f",height) + " m";
        canvas.drawText(heightText,startX,startY + spacing*3,bluePaint);

        //设置水平速度相关文本
        String horizontalText = "H.S" + "  " + String.format("%.1f",horizontalSpeed) + " m/s";
        canvas.drawText(horizontalText,startX,startY + spacing*4,bluePaint);

        //设置垂直速度相关文本
        String verticalText = "V.S" + "  " + String.format("%.1f",verticalSpeed) + " m/s";
        canvas.drawText(verticalText,startX,startY + spacing*5,bluePaint);

        //设置经度相关文本
        String longitudeText = "Lng" + "  " + String.format("%.7f",lng);
        canvas.drawText(longitudeText,startX,startY + spacing*6,bluePaint);

        //设置纬度相关文本
        String latitudeText = "Lat" + "  " + String.format("%.7f",lat);
        canvas.drawText(latitudeText,startX,startY + spacing*7,bluePaint);

        // 重绘视图
        invalidate();
    }


}

